home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / files / example2.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  4KB  |  134 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Files                       Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-11                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will reads ten integer values from an */
  21. /* already existing file called "HighScore.dat" which */
  22. /* is located on the RAM disk. (This file was created */
  23. /* by Example1.)                                      */
  24.  
  25.  
  26.  
  27. /* Include the dos library definitions: */
  28. #include <dos/dos.h>
  29.  
  30. /* Now we include the necessary function prototype files:         */
  31. #include <clib/dos_protos.h>       /* General dos functions...    */
  32. #include <stdio.h>                 /* Std functions [printf()...] */
  33. #include <stdlib.h>                /* Std functions [exit()...]   */
  34.  
  35.  
  36.  
  37. /* Set name and version number: */
  38. UBYTE *version = "$VER: AmigaDOS/InputOutput/Example2 1.0";
  39.  
  40.  
  41.  
  42. /* Declared our own function(s): */
  43.  
  44. /* Our main function: */
  45. int main( int argc, char *argv[] );
  46.  
  47.  
  48.  
  49. /* Main function: */
  50.  
  51. int main( int argc, char *argv[] )
  52. {
  53.   /* A "BCPL" pointer to our file: */
  54.   BPTR my_file;
  55.  
  56.   /* The numbers: (10 integers will be collected) */
  57.   int my_highscore[ 10 ];
  58.  
  59.   /* Store here the number of bytes actually collected: */
  60.   long bytes_read;
  61.  
  62.   /* A simple loop variable: */
  63.   int loop; 
  64.  
  65.  
  66.  
  67.   /* Since we want to collect some data from an already existing */
  68.   /* file we must open the file "HighScore.dat" as an old file:  */
  69.   my_file = Open( "RAM:HighScore.dat", MODE_OLDFILE );
  70.   
  71.   /* Have we opened the file successfully? */
  72.   if( !my_file )
  73.   {
  74.     /* Inform the user: */
  75.     printf( "Error! Could not open the file!\n" );
  76.  
  77.     /* Exit with an error code: */
  78.     exit( 20 );
  79.   }
  80.  
  81.   /* The file has now been opened: */
  82.   printf( "File open!\n" );
  83.  
  84.  
  85.  
  86.   /* Load the values: */
  87.   printf( "Loading values...\n" );
  88.  
  89.   /* Collect 10 integers (40 bytes): */
  90.   bytes_read = Read( my_file, my_highscore, sizeof( my_highscore ) );
  91.  
  92.   /* Did we get all data? */ 
  93.   if( bytes_read != sizeof( my_highscore ) )
  94.   {
  95.     /* No! We could not read all values! */
  96.     printf( "Error! Could read all values!\n" );
  97.  
  98.     /* Close the file: */
  99.     Close( my_file );
  100.  
  101.     /* Exit with an error code: */
  102.     exit( 21 );
  103.   }
  104.   else
  105.   {
  106.     /* OK! */
  107.     printf( "All values were successfully collected!\n" );
  108.   }
  109.  
  110.  
  111.  
  112.   /* Print the values: */
  113.   for( loop=0; loop < 10; loop++ )
  114.     printf( "Highscore[%d] = %8d\n", loop, my_highscore[ loop ] );
  115.  
  116.  
  117.  
  118.   /* Close the file: */
  119.   if( Close( my_file ) )
  120.     printf( "File closed!\n" );
  121.   else
  122.     printf( "Error! File could not be closed!\n" );
  123.  
  124.   /* Remember that even if the file could not be */
  125.   /* closed we must NOT try to close it again!   */
  126.  
  127.  
  128.  
  129.   /* The End! */
  130.   exit( 0 );
  131. }
  132.  
  133.  
  134.